using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_ArticleManagement : admin_BaseUserControl
{
    ArticleDAO daoArticle = new ArticleDAO();
    Article art = new Article();
    int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        ActiveAccording = "article";
        if (Request["mode"] == "1") //mode: edit
        {            
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
            try
            {
                id = int.Parse(Request["id"]);
                CurrentID = Request["id"];
                Article art = daoArticle.GetArticle(id);
                txtTitle.Text = art.Title;
                txtTag.Text = art.Tag;
                fckContent.Value = art.Content;
                chkState.Checked = art.State;
            }
            catch (Exception ex)
            {
                Response.Redirect("Default.aspx?ctrl=art_list");
            }
        }
        else //mode: add
        {
            lnkAdd.Visible = true;
            lnkUpdate.Visible = false;
            lnkDelete.Visible = false;
        }
    }
    /// <summary>
    /// Create new article
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkAdd_Click(object sender, EventArgs e)
    {      
        art.Title = txtTitle.Text;
        art.Content = fckContent.Value;
        art.Tag = txtTag.Text;
        art.ModifiedDate = DateTime.Now;
        art.ModifiedBy = 1;
        art.State = chkState.Checked;
        if (daoArticle.Insert(art) > 0)
        {
            ShowMessage("New article has been created successfully");
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
            CurrentID = art.Id.ToString();
        }
        else ShowMessage("Error occured when trying to create new article.");
    }
    protected void lnkUpdate_Click(object sender, EventArgs e)
    {
        try
        {
           if(id==0) id = int.Parse(CurrentID);
            art.Id = id;
            art.Title = txtTitle.Text;
            art.Tag = txtTag.Text;
            art.Content = fckContent.Value;
            art.ModifiedBy = 1;
            art.ModifiedDate = DateTime.Now;
            art.State = chkState.Checked;
            if (daoArticle.Update(art) > 0)
            {
                ShowMessage("Article is updated");
            }
            else ShowMessage("Error occured when trying to update article");
        }
        catch (Exception ex)
        {
            ShowMessage("Error occured when trying to update article");
        }
    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        if (id == 0) id = int.Parse(CurrentID);
        art.Id = id;
        if (daoArticle.Delete(art.Id) > 0)
        {
            ShowMessage("Article is deleted");
            lnkAdd.Visible = true;
            lnkUpdate.Visible = false;
            lnkDelete.Visible = false;
        }
        else ShowMessage("Error occurred. Deleting failed.");
    }
}
